package com.xixing.weixin.response;


import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

@RooJavaBean
@RooToString
public class Response {
    private static final Logger LOGGER = LoggerFactory.getLogger(Response.class);
    private transient  List<String> aliases = new ArrayList<String>();
    private String toUserName;


    private String fromUserName;


    private Long createTime;

    // text
    private String msgType;

    // 响应：星标.
    private Integer funcFlag;

    public String toXml() {
        XStream xstream = new XStream(new XppDriver() {
            public HierarchicalStreamWriter createWriter(Writer out) {
                return new PrettyPrintWriter(out) {
                    public void startNode(String name){
                        //将对应的属性名 转化成首字母大写.
                        String nodeName = name;
                        if(!aliases.contains(name))
                            nodeName = StringUtils.capitalize(name);
                        super.startNode(nodeName);
                    }
                };
            }
        });

        prepare(xstream);
        return xstream.toXML(this);
    }

    protected void alias(XStream xstream, String name, Class type) {
        xstream.alias(name, type);
        aliases.add(name);
    }

    protected void prepare(XStream xstream){
        alias(xstream, "xml", this.getClass());
    }
}
